//
//  ViewController.m
//  WeiboAD
//
//  Created by fee on 16/7/15.
//  Copyright © 2016年 com.liu.weiboAD. All rights reserved.
//

#import "LaunchViewController.h"
#import "WeiboTabViewController.h"
#import "AppDelegate.h"
#import "User.h"

@interface LaunchViewController ()
@end

@implementation LaunchViewController
{
    AppDelegate *myDelegate;
    WBSDKRelationshipButton *relationshipButton;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    relationshipButton = [[WBSDKRelationshipButton alloc] initWithFrame:CGRectMake(20, 550, 140, 30) accessToken:myDelegate.wbtoken currentUser:myDelegate.wbCurrentUserID followUser:@"2002619624" completionHandler:^(WBSDKBasicButton *button, BOOL isSuccess, NSDictionary *resultDict) {
        
        NSString* accessToken = [resultDict objectForKey:@"access_token"];
        if (accessToken)
        {
            myDelegate.wbtoken = accessToken;
        }
        NSString* uid = [resultDict objectForKey:@"uid"];
        if (uid)
        {
            myDelegate.wbCurrentUserID = uid;
        }
    }];
   

  // Do any additional setup after loading the view, typically from a nib.
}

-(void)postWithUser:(NSDictionary *)dic{
    if(dic[@"access_token"]==NULL){
        [MBProgressHUD showError:@"认证失败"];
    }else{
    XCHTTPManager *manager = [XCHTTPManager shareManager];
    NSString *url = [NSString stringWithFormat:@"%@%@",ApiHead,ApiUploadAccessToken];
    [UserDefault setValue:dic[@"access_token"] forKey:@"access_token"];
    [UserDefault setValue:dic[@"uid"] forKey:@"uid"];
    [manager post:url params:dic success:^(id responseObj) {
        //[MBProgressHUD showSuccess:@"上传accessToken成功"];
        NSDictionary *dics = responseObj;
        WeiboTabViewController *weiboTabVC = [[WeiboTabViewController alloc]initWithUser:dics];
        [myDelegate.window setRootViewController:weiboTabVC];
    } failured:^(NSError *error) {
        [MBProgressHUD showError:@"请求失败"];
    }];
    }
   }

- (IBAction)weiboLoginAction:(id)sender {
    if ([UserDefault getValueOfKey:@"access_token"]&&[UserDefault getValueOfKey:@"uid"]) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"access_token"] = [UserDefault getValueOfKey:@"access_token"];
        dic[@"uid"] = [UserDefault getValueOfKey:@"uid"];
        [self postWithUser:dic];
    }else{
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = ApigetAuth;
    request.scope = @"all";
    [WeiboSDK sendRequest:request];
 }
}




//-(void)weiboLoginByResponse:(WBBaseResponse *)response{
//    NSDictionary *dic = (NSDictionary *) response.requestUserInfo;
//    [self postWithUser:dic];
//    NSLog(@"userinfo9 %@",dic[@"access_token"]);
//    
//}

//- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result
//{
//    NSString *title = nil;
//    UIAlertView *alert = nil;
//    
//    title = NSLocalizedString(@"收到网络回调9", nil);
//    alert = [[UIAlertView alloc] initWithTitle:title
//                                       message:[NSString stringWithFormat:@"%@",result]
//                                      delegate:nil
//                             cancelButtonTitle:NSLocalizedString(@"确定9", nil)
//                             otherButtonTitles:nil];
//    [alert show];
//}
//
//- (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error;
//{
//    NSString *title = nil;
//    UIAlertView *alert = nil;
//    
//    title = NSLocalizedString(@"请求异常9", nil);
//    alert = [[UIAlertView alloc] initWithTitle:title
//                                       message:[NSString stringWithFormat:@"%@",error]
//                                      delegate:nil
//                             cancelButtonTitle:NSLocalizedString(@"确定9", nil)
//                             otherButtonTitles:nil];
//    [alert show];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
